﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using CMISLibrary.Entities;
using BCNPCObject.DB;


namespace BCNPCObject
{
    public class cls_NPC_TRTH_CHISO_Controller
    {
         #region   Atributes

        private DataSet CMIS_Header = new CMISOutputParameter();
        private DataSet ds = new DataSet();
        private DataTable dt = new DataTable();
        private NPC_TRTH_CHISO info;

        #endregion

        #region   Constructor

        public cls_NPC_TRTH_CHISO_Controller()
        {
            info = new NPC_TRTH_CHISO();
        }
        public cls_NPC_TRTH_CHISO_Controller(NPC_TRTH_CHISO Info)
        {
            this.info = Info;
        }

        #endregion

        #region   Properties

        public NPC_TRTH_CHISO pInfor
        {
            get { return info; }
            set { info = value; }
        }

        #endregion

        #region Method

        public IEnumerable<NPC_TRTH_CHISO> select_NPC_TRTH_CHISO_All(string strMadviqly)
        {
            try
            {
                CMIS2.ResetDB();
                var q = from p in CMIS2.DB.NPC_TRTH_CHISO
                        where p.MA_DVIQLY == strMadviqly
                        orderby p.ID_TRTH
                        select p;

                return q;
            }
            catch
            {
                CMIS2.ResetDB(); return null;
            }
        }

        public IEnumerable<NPC_TRTH_CHISO> select_NPC_TRTH_CHISO_by_id_truythu(long intID)
        {
            try
            {
                CMIS2.ResetDB();
                var q = from p in CMIS2.DB.NPC_TRTH_CHISO
                        where p.ID_TRTH == intID
                        orderby p.ID_CHISO descending
                        select p;
                return q;
            }
            catch
            {
                CMIS2.ResetDB();
                return null;
            }
        }

        public IEnumerable<NPC_TRTH_CHISO> kythangnam(long id_trth)
        {
            try
            {
                CMIS2.ResetDB();

                var gcsCHISO = (from p in CMIS2.DB.NPC_TRTH_CHISO
                                where p.ID_TRTH == id_trth
                                && p.KY != 0
                                select p).ToList();

                return gcsCHISO;

            }
            catch
            {
                CMIS2.ResetDB();
                return null;
            }
        }

        public bool insert_NPC_TRTH_CHISO(ref string strErr)
        {
            try
            {
                NPC_TRTH_CHISO objNPC_TRTH_CHISO = new NPC_TRTH_CHISO()
                {
                    ID_CHISO = info.ID_CHISO,
                    MA_DVIQLY = info.MA_DVIQLY , 
                    ID_TRTH = info.ID_TRTH , 
                    CHISO_CU = info.CHISO_CU , 
                    CHISO_MOI = info.CHISO_MOI , 
                    SAN_LUONG = info.SAN_LUONG , 
                    NGAY_DKY = info.NGAY_DKY , 
                    GIO_DKY = info.GIO_DKY , 
                    PHUT_DKY = info.PHUT_DKY , 
                    NGAY_CKY = info.NGAY_CKY , 
                    GIO_CKY = info.GIO_CKY , 
                    PHUT_CKY = info.PHUT_CKY , 
                    HSN = info.HSN , 
                    BCS = info.BCS , 
                    SO_CTO = info.SO_CTO , 
                    MA_CTO = info.MA_CTO , 
                    KY = info.KY , 
                    LOAI_CHISO = info.LOAI_CHISO ,
                    NGUOI_TAO = info.NGUOI_TAO, 
                    NGAY_TAO = info.NGAY_TAO

                };
                CMIS2.DB.NPC_TRTH_CHISO.InsertOnSubmit(objNPC_TRTH_CHISO);
                CMIS2.DB.SubmitChanges();
                CMIS2.ResetDB();
                return true;
            }
            catch (Exception ex)
            {
                strErr = ex.Message;
                CMIS2.ResetDB(); return false;
            }
        }

        public bool update_NPC_TRTH_CHISO(ref string strErr)
        {
            try
            {
                NPC_TRTH_CHISO objNPC_TRTH_CHISO = CMIS2.DB.NPC_TRTH_CHISO.Single(c => c.ID_CHISO == info.ID_CHISO && c.MA_DVIQLY == info.MA_DVIQLY);

                objNPC_TRTH_CHISO.ID_TRTH = info.ID_TRTH ;
                objNPC_TRTH_CHISO.CHISO_CU = info.CHISO_CU ;
                objNPC_TRTH_CHISO.CHISO_MOI = info.CHISO_MOI ;
                objNPC_TRTH_CHISO.SAN_LUONG = info.SAN_LUONG ;
                objNPC_TRTH_CHISO.NGAY_DKY = info.NGAY_DKY ;
                objNPC_TRTH_CHISO.GIO_DKY = info.GIO_DKY ;
                objNPC_TRTH_CHISO.PHUT_DKY = info.PHUT_DKY ;
                objNPC_TRTH_CHISO.NGAY_CKY = info.NGAY_CKY ;
                objNPC_TRTH_CHISO.GIO_CKY = info.GIO_CKY ;
                objNPC_TRTH_CHISO.PHUT_CKY = info.PHUT_CKY ;
                objNPC_TRTH_CHISO.HSN = info.HSN ;
                objNPC_TRTH_CHISO.BCS = info.BCS ;
                objNPC_TRTH_CHISO.SO_CTO = info.SO_CTO ;
                objNPC_TRTH_CHISO.MA_CTO = info.MA_CTO ;
                objNPC_TRTH_CHISO.KY = info.KY ;
                objNPC_TRTH_CHISO.LOAI_CHISO = info.LOAI_CHISO ;
                objNPC_TRTH_CHISO.NGUOI_TAO = info.NGUOI_TAO ;
                objNPC_TRTH_CHISO.NGAY_TAO = info.NGAY_TAO ;

                CMIS2.DB.SubmitChanges();

                return true;
            }
            catch(Exception ex)
            {
                strErr = ex.Message;
                CMIS2.ResetDB(); return false;
            }
        }

        public bool delete_NPC_TRTH_CHISO(ref string strErr)
        {
            try
            {
                var query = from p in CMIS2.DB.NPC_TRTH_CHISO where p.ID_CHISO == info.ID_CHISO && p.MA_DVIQLY == info.MA_DVIQLY select p;
                if (query != null && query.Count() > 0)
                {
                    CMIS2.DB.NPC_TRTH_CHISO.DeleteAllOnSubmit(query.ToList());
                    CMIS2.DB.SubmitChanges();
                    return true;
                }
                return true;
            }
            catch (Exception ex)
            {
                strErr = ex.Message;
                CMIS2.ResetDB(); return false;
            }
        }

        public long GetNextSEQ_NPC_TRTHU_CHISO(ref string strErr)
        {
            try
            {
                long _ID = CMIS2.DB.ExecuteCommand("select SEQ_NPC_TRTHU_CHISO.nextval from dual", ref strErr);

                if (strErr == "") return _ID;

                else return -1;
            }
            catch
            {
                return -1;
            }
        }

        #endregion
    }
}
